/**********************************************
** @Des: This file ...
** @Author: haodaquan
** @Date:   2017-09-08 00:18:02
** @Last Modified by:   haodaquan
** @Last Modified time: 2017-09-16 17:26:48
***********************************************/

package models

import (
	//"net/url"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	//_ "github.com/go-sql-driver/mysql"
	"github.com/astaxie/beego/logs"
	_ "github.com/lib/pq"
)

func Init() {
	////mysql db server param
	//dbhost := beego.AppConfig.String("db.host")
	//dbport := beego.AppConfig.String("db.port")
	//dbuser := beego.AppConfig.String("db.user")
	//dbpassword := beego.AppConfig.String("db.password")
	//dbname := beego.AppConfig.String("db.name")
	//timezone := beego.AppConfig.String("db.timezone")
	//if dbport == "" {
	//	dbport = "3306"
	//}
	//dsn := dbuser + ":" + dbpassword + "@tcp(" + dbhost + ":" + dbport + ")/" + dbname + "?charset=utf8"
	//// fmt.Println(dsn)
	//
	//if timezone != "" {
	//	dsn = dsn + "&loc=" + url.QueryEscape(timezone)
	//}
	//postgres_sql dbstr
	pgstr := beego.AppConfig.String("dbstring")

	orm.RegisterDataBase("default", "postgres", pgstr)
	orm.RegisterModel(new(Auth), new(Role), new(RoleAuth), new(Admin), new(AiCdr), new(AiFlowContent),
		new(Group), new(Env), new(Code), new(Api), new(ApiDetail), new(AiProcess), new(RunTimeModel),
		new(AiProcessContent), new(ApiParam), new(AiTpl), new(AiTask), new(AiNumbers), new(AIVoiceLibrary),
		new(NumberGroup), new(NumberGroupMap), new(AiNotMatch), new(GroupMap), new(AiServers), new(GateWay),
		new(GatewayGroup), new(AiExtansion), new(AiSeatGroup), new(AiSeatTask), new(AiSeatGroupMap), new(AiSeat),
		new(AiRoute), new(AiRouteDetails))

	if beego.AppConfig.String("runmode") == "dev" {
		orm.Debug = true
	}
	//配置日志
	logs.SetLogger(logs.AdapterFile, `{"filename":"`+beego.AppConfig.String("log_file")+`"}`)

}

func TableName(name string) string {
	return beego.AppConfig.String("db.prefix") + name
}

func ErrorLog(err error) {
	logs.Error(err)
}

func Eqint64(a, b int64) bool {
	return a == b
}
